void buttonSetup()
{
  int pos[] = {screenSize[0]/2, screenSize[1]-50};
  globalButtonContainer = new tButtonContainer(pos);
  globalButtonContainer.addButton(createBackToMenuButton());
  globalButtonContainer.addButton(createSubmitButton());
  globalButtonContainer.alignButtons();
}

class tButtonContainer
{
  tButton buttons[];
  // 
  int pos[];
  
  tButtonContainer(int[] newPos)
  {
    pos = newPos;
  }
  
  void addButton(int[][] newCoords, color newColor, String newName, String newOnClickFunction)
  {
    tButton newButton = new tButton(newCoords, newColor, newName, newOnClickFunction);
    this.addButton(newButton);
  }
  
  void addButton(tButton newButton)
  {
    tButton newButtons[] = null;
    
    if (buttons == null)
    {
      newButtons = new tButton[1];
    }
    else
    {
      newButtons = new tButton[buttons.length+1];
      for (int i = 0; i < buttons.length; i++)
      {
        newButtons[i] = buttons[i];
      }
    }
    newButtons[newButtons.length-1] = newButton;
    buttons = newButtons;
  }
  
  /**
   * Aligns all buttons on a horizontal line centered on pos[]
   * centers on pos
   */
  void alignButtons()
  {
    int margin = 20;
    
    int totalWidth = 0;
    for (int i = 0; i < buttons.length; i++)
    {
      totalWidth += margin + (buttons[i].coords[1][0] - buttons[i].coords[0][0]);
    }
    totalWidth -= margin;
    
    int runningWidth = 0;
    for (int i = 0; i < buttons.length; i++)
    {
      int buttonWidth = buttons[i].coords[1][0] - buttons[i].coords[0][0];
      int buttonHeight = buttons[i].coords[2][1] - buttons[i].coords[0][1];
      int newPos[] = { pos[0] - totalWidth/2 + runningWidth, pos[1] - buttonHeight/2 };
      buttons[i].setPosition(newPos);
      runningWidth += margin + buttonWidth;
    }
  }
  
  /**
   * removes all buttons
   */
  void clearButtons()
  {
    buttons = null;
  }
  
  void update(int[] mousePos)
  {
    for (int i = 0; i < buttons.length; i++)
    {
      buttons[i].update(mousePos);
    }
  }
  
  boolean doDraw()
  {
    for (int i = 0; i < buttons.length; i++)
    {
      if (!buttons[i].doDraw())
      {
        return false;
      }
    }
    return true;
  }
}

class tButton extends tShape
{
  String myName;
  boolean pressed;
  String onClickFunction;
  color textColor;
  
  tButton(int[][] newCoords, color newColor, String newName, String newOnClickFunction)
  {
    super(newCoords, newColor);
    myName = newName;
    pressed = false;
    onClickFunction = newOnClickFunction;
    textColor = color(255-red(newColor), 255-green(newColor), 255-blue(newColor));
  }
  
  void update(int[] mousePos)
  {
    super.update(mousePos, false);
    if (getMouseDownLeft() && (frameOnWhichMouseWasLastPressed-frameCount) < 3)
    {
      if (pressed == false && hovered)
      {
        pressed = true;
      }
    }
    else
    {
      if (pressed && hovered)
      {
        this.onClick();
      }
      pressed = false;
    }
  }
  
  void onClick()
  {
    println("I was clicked!");
    if (onClickFunction == "submit")
    {
      submit();
    }
    else if (onClickFunction == "backToMenu")
    {
      backToMenu();
    }
  }
  
  boolean doDraw()
  {
    if (pressed && hovered)
    {
      drawTexture = 2;
    }
    if (!super.doDraw())
    {
      return false;
    }
    fill(textColor);
    writeLargeText(myName, position[0]+coords[0][0]+(coords[2][0]-coords[0][0])/2, position[1]+coords[0][1]+(coords[2][1]-coords[0][1])/2 + 8);
    return true;
  }
  
  void writeLargeText(String toWrite, int x, int y)
  {
    PFont font = menuFont;
    textFont(font);
    text(toWrite, x-textWidth(toWrite)/2, y);
  }
  
  void setTextColor(color newColor)
  {
    textColor = newColor;
  }
}

tButton createSubmitButton()
{
  int halfw = screenSize[0]/2;
  int w = 200;
  int h = 50;
  int halfh = screenSize[1]-50;
  int coords[][] = { {0,0}, {w,0}, {w,h}, {0,h} };
  tButton retval = new tButton(coords, color(155,155,155), "Submit", "submit");
  retval.setTextColor(color(255,255,255));
  return retval;
}

tButton createBackToMenuButton()
{
  int halfw = screenSize[0]/2;
  int w = 200;
  int h = 50;
  int halfh = screenSize[1]-50;
  int coords[][] = { {0,0}, {w,0}, {w,h}, {0,h} };
  tButton retval = new tButton(coords, color(155,155,155), "Back To Menu", "backToMenu");
  retval.setTextColor(color(255,255,255));
  return retval;
}

void submit()
{
    fill(0);
    rect(162,100,970,600);
    globalShapeContainer.doDraw();
    PImage solvedSandbox = ripSandbox();
    PImage completedOutline = convertToBW(solvedSandbox);
    completedOutline.save("levels/userSol.png");
    PImage solution = loadImage("levels/temp.png");
    if(sameImage(completedOutline,solution))
     print("CORRECT SOLUTION!");
    else
     print("KEEP TRYING!");
}

void backToMenu()
{
  gameState = 1;
}
